package Demo1;

class Money implements Cloneable{
    public double m = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


class Person implements Cloneable{
    public String name;
    public  int age;

    public Money money = new Money();

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //这里面在克隆person  但是我没看到你克隆money，那么怎么克隆呢？

        Person tmp = (Person)super.clone();
        tmp.money = (Money) this.money.clone();
        return tmp;
        //return super.clone();
    }

}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException{
        Person person1 = new Person("张三",10);
        Person person2 = (Person)person1.clone();  //person1.克隆()
        System.out.println("person "+person1.money.m);
        System.out.println("person2 "+person2.money.m);
        System.out.println("========================");
        person1.money.m = 99.99;
        System.out.println("person "+person1.money.m);
        System.out.println("person2 "+person2.money.m);
    }


    public static void main1(String[] args) throws CloneNotSupportedException{
        Person person1 = new Person("张三",10);
        Person person2 = (Person)person1.clone();  //person1.克隆()
        System.out.println(person2.toString());
        System.out.println(person1.toString());
    }
}
